package net.shopxx.action.admin;

import javax.annotation.Resource;

import net.shopxx.bean.Pager;
import net.shopxx.bean.Pager.OrderType;
import net.shopxx.entity.ProStaff;
import net.shopxx.service.ProStaffService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 生产人员
 */

@ParentPackage("admin")
public class ProStaffAction extends BaseAdminAction {

	private static final long serialVersionUID = -9037144845778915325L;

	private ProStaff proStaff;

	@Resource
	private ProStaffService proStaffService;
	
	// 是否Name已存在 ajax验证
	public String checkCode() {
		String oldValue = getParameter("oldValue");
		String newValue = proStaff.getCode();
		if (proStaffService.isUnique("code", oldValue, newValue)) {
			return ajaxText("true");
		} else {
			return ajaxText("false");
		}
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		proStaff = proStaffService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		if (pager == null) {
			pager = new Pager();
			pager.setOrderType(OrderType.asc);
			pager.setOrderBy("name");
		}
		pager = proStaffService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() {
		proStaffService.delete(ids);
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "proStaff.name", message = "姓名不允许为空!")
		}, 
		requiredFields = {
			@RequiredFieldValidator(fieldName = "proStaff.code", message = "人员编号不允许为空!")
		}
	)
	@InputConfig(resultName = "error")
	public String save() {
//		proStaff.setDeliveryTypeSet(null);
		proStaffService.save(proStaff);
		redirectionUrl = "pro_staff!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(fieldName = "proStaff.name", message = "姓名不允许为空!")
				}, 
				requiredFields = {
					@RequiredFieldValidator(fieldName = "proStaff.code", message = "人员编号不允许为空!")
				}

	)
	@InputConfig(resultName = "error")
	public String update() {
		ProStaff persistent = proStaffService.load(id);
		BeanUtils.copyProperties(proStaff, persistent, new String[]{"id", "createDate", "modifyDate"});
//		BeanUtils.copyProperties(proStaff, persistent);
		proStaffService.update(persistent);
		redirectionUrl = "pro_staff!list.action";
		return SUCCESS;
	}

	public ProStaff getProStaff() {
		return proStaff;
	}

	public void setProStaff(ProStaff proStaff) {
		this.proStaff = proStaff;
	}



}